package com.flowcanto.gs1demo.server;

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;

import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;

//A Webserver waits for clients to connect, then starts a separate
//thread to handle the request.
public class Webserver {
	static ServerSocket serverSocket;
	static private int PORT = 8088; 
	protected String appDir;

	public Webserver() {
		appDir = new File("foo").getAbsoluteFile().getParentFile().getPath().replaceAll("\\\\","\\/") + '/' ;
		Server server = new Server(PORT);    
		Context root = new Context(server,"/",Context.SESSIONS|Context.NO_SECURITY);
		root.addServlet(new ServletHolder(new OrderHandler(this)), "/place_order");
		root.addServlet(new ServletHolder(new FileListCreator(this)), "/get_message_list");
		root.addServlet(new ServletHolder(new GetFile(this)), "/get_message");

		try {
			server.start();
		} catch (Exception e) {
			throw new Error(e);
		}
		System.out.println("Webserver running with root: " + appDir);
	}

	
	
	public static void main(String[] args) throws IOException {
		new Webserver();
	}

	public String applicationDirectory() {
		return appDir;
	}
	
}

